package com.tengju.bff.interfaces.manage.crm.dashborad;

import com.tengju.bff.interfaces.covercharge.util.ExcelExportUtil;
import com.tengju.bff.interfaces.manage.crm.dashborad.facade.SecPerformanceObjectiveFacade;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.SecPerformanceObjectiveExportForm;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.SecPerformanceObjectiveFormPageQuery;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.SecTeamPerformanceObjectiveExportForm;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.SecTeamPerformanceObjectiveFormPageQuery;
import com.tengju.bff.interfaces.shared.*;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.support.domain.annotation.BizLog;
import com.tengju.support.domain.annotation.BizLogSysName;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: 李大仙
 * @date: 2021/5/6 10:42 上午
 */
@Validated
@RestController
@Api(tags = "生意参谋-SEC绩效目标")
@RequestMapping(value = "manage/crm/sec/performance/objective/")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class SecPerformanceObjectiveController {

    private final SecPerformanceObjectiveFacade secPerformanceObjectiveFacade;

    @Value("#{'${crm.performance.objective.sec}'.split(',')}")
    private String[] performanceObjectives;

    @ApiOperation(value = "我的目标（KA）", notes = "我的目标（KA）")
    @RequestMapping(value = "/ka/allTarget", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<KVVO>> kaAllTarget(@NotNull(message = "日期类型不能为空") @RequestParam("dateType") Integer dateType,
                                               @NotBlank(message = "日期不能为空") @RequestParam("date") String date) {
        return ApiResponse.newSuccess(secPerformanceObjectiveFacade.kaAllTarget(dateType, date));
    }

    @ApiOperation(value = "目标达成SEC更新时间", notes = "目标达成SEC更新时间")
    @RequestMapping(value = "/ka/allTarget/update/time", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<KVVO<String,String>> getSECTargetUpdateTime(@NotNull(message = "日期类型不能为空") @RequestParam("dateType") Integer dateType,
                                               @NotBlank(message = "日期不能为空") @RequestParam("date") String date) {
        return ApiResponse.newSuccess(secPerformanceObjectiveFacade.getSECTargetUpdateTime(dateType,date));
    }


    @ApiOperation(value = "sec绩效目标列表", notes = "sec绩效目标列表")
    @RequestMapping(value = "/sec/allTargetTable", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> secAllTargetTable(@Valid SecPerformanceObjectiveFormPageQuery query) {
        String meta = "motivation_target_sec";
        return ApiResponse.newSuccess(secPerformanceObjectiveFacade.secAllTargetTable(query,meta));
    }



    @ApiOperation(value = "sec下服务商绩效目标列表", notes = "sec下服务商绩效目标列表")
    @RequestMapping(value = "/sec/teamAllTargetTable", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> teamAllTargetTable(@Valid SecTeamPerformanceObjectiveFormPageQuery query) {
        return ApiResponse.newSuccess(secPerformanceObjectiveFacade.teamAllTargetTable(query,"motivation_target_star"));
    }


    @BizLog(sysName = BizLogSysName.TENGJU_CRM)
    @ApiOperation(value = "sec绩效目标列表导出", notes = "sec绩效目标列表导出")
    @RequestMapping(value = "/sec/allTarget/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void secAllTargetExport(@Valid SecPerformanceObjectiveExportForm query) throws Exception {
        SecPerformanceObjectiveFormPageQuery pageQuery = new SecPerformanceObjectiveFormPageQuery();
        pageQuery.setDateType(query.getDateType());
        pageQuery.setDate(query.getDate());
        pageQuery.setSecIdCode(query.getSecIdCode());
        pageQuery.setPageIndex(1);
        pageQuery.setPageSize(50000);

        HttpServletRequest request = ServletContextUtil.getRequest();
        HttpServletResponse response = ServletContextUtil.getResponse();
        ExcelExportUtil excel = new ExcelExportUtil(1, "目标达成sec列表");
        TableVO tableVO = secPerformanceObjectiveFacade.secAllTargetTable(pageQuery,"motivation_target_export");
        excel.create(0,0,0,tableVO);
        String fileName = "目标达成sec列表" + "_" + DateUtil.formatDayInt(LocalDateTime.now())  + ".xlsx";
        excel.downloadExcel(request,response,fileName);

    }


    @BizLog(sysName = BizLogSysName.TENGJU_CRM)
    @ApiOperation(value = "目标达成(服务商)列表导出", notes = "目标达成(服务商)列表导出")
    @RequestMapping(value = "/sec/teamAllTarget/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void teamAllTargetExport(@Valid SecTeamPerformanceObjectiveExportForm query) throws Exception {

        SecTeamPerformanceObjectiveFormPageQuery pageQuery = new SecTeamPerformanceObjectiveFormPageQuery();
        pageQuery.setDateType(query.getDateType());
        pageQuery.setDate(query.getDate());
        pageQuery.setSecIdCode(query.getSecIdCode());
        pageQuery.setIdCode(query.getIdCode());
        pageQuery.setIsDirect(query.getIsDirect());
        pageQuery.setPageIndex(1);
        pageQuery.setPageSize(50000);

        HttpServletRequest request = ServletContextUtil.getRequest();
        HttpServletResponse response = ServletContextUtil.getResponse();
        ExcelExportUtil excel = new ExcelExportUtil(1, "目标达成服务商列表");
        TableVO tableVO = secPerformanceObjectiveFacade.teamAllTargetTable(pageQuery,"motivation_target_star_export");
        excel.create(0,0,0,tableVO);

        String fileName = "目标达成服务商列表" + "_" + DateUtil.formatDayInt(LocalDateTime.now()) + ".xlsx";
        excel.downloadExcel(request,response,fileName);
    }
}
